package cn.iocoder.yudao.module.scrm.service.schedules;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.PersonInfo;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.SchedulesCustomerPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.SchedulesCustomerSaveReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.SchedulesCustomerUpdateReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.schedules.SchedulesDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.schedulescustomer.SchedulesCustomerDO;
import cn.iocoder.yudao.module.scrm.dal.mysql.schedulescustomer.SchedulesCustomerMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.scrm.enums.ErrorCodeConstants.SCHEDULES_CUSTOMER_NOT_EXISTS;
import static cn.iocoder.yudao.module.scrm.enums.ErrorCodeConstants.SCHEDULES_NOT_ENABLE;

/**
 * 排期客户 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class SchedulesCustomerServiceImpl implements SchedulesCustomerService {

    @Resource
    private SchedulesCustomerMapper schedulesCustomerMapper;

    @Resource
    private SchedulesService schedulesService;

    @Override
    public Boolean createSchedulesCustomer(SchedulesCustomerSaveReqVO createReqVO) {
        // 插入
        List<SchedulesCustomerDO> schedulesCustomers = convertToSchedulesCustmerDOList(createReqVO.getSchedulesId(), createReqVO.getCustomerList());
        // 返回
        return schedulesCustomerMapper.insertBatch(schedulesCustomers);
    }

    @Override
    public void updateSchedulesCustomer(SchedulesCustomerUpdateReqVO updateReqVO) {
        // 校验存在
        SchedulesCustomerDO schedulesCustomerDO = validateSchedulesCustomer(updateReqVO.getId());
        // 查看排期状态
        SchedulesDO schedulesDO = schedulesService.getSchedules(schedulesCustomerDO.getSchedulesId());
        if(schedulesDO.getStatus() != 0) {
            throw exception(SCHEDULES_NOT_ENABLE);
        }
        // 更新
        SchedulesCustomerDO updateObj = BeanUtils.toBean(updateReqVO, SchedulesCustomerDO.class);
        schedulesCustomerMapper.updateById(updateObj);
    }

    @Override
    public void deleteSchedulesCustomer(Long id) {
        // 校验存在
        validateSchedulesCustomerExists(id);
        // 删除
        schedulesCustomerMapper.deleteById(id);
    }

    private void validateSchedulesCustomerExists(Long id) {
        if (schedulesCustomerMapper.selectById(id) == null) {
            throw exception(SCHEDULES_CUSTOMER_NOT_EXISTS);
        }
    }

    private SchedulesCustomerDO validateSchedulesCustomer(Long id) {
        SchedulesCustomerDO schedulesCustomerDO = schedulesCustomerMapper.selectById(id);
        if (schedulesCustomerDO == null) {
            throw exception(SCHEDULES_CUSTOMER_NOT_EXISTS);
        }
        return schedulesCustomerDO;
    }

    @Override
    public SchedulesCustomerDO getSchedulesCustomer(Long id) {
        return schedulesCustomerMapper.selectById(id);
    }

    @Override
    public PageResult<SchedulesCustomerDO> getSchedulesCustomerPage(SchedulesCustomerPageReqVO pageReqVO) {
        return schedulesCustomerMapper.selectPage(pageReqVO);
    }

    @Override
    public Long getCustomerCountBySchedulesId(Long schedulesId) {
        return schedulesCustomerMapper.selectCount(SchedulesCustomerDO::getSchedulesId, schedulesId);
    }

    private List<SchedulesCustomerDO> convertToSchedulesCustmerDOList(Long schedulesId, List<PersonInfo> customerInfos) {
        return customerInfos.stream()
                .map(Optional::ofNullable)
                .filter(Optional::isPresent)
                .map(Optional::get)
                .filter(customerInfo -> customerInfo.getId() != null && customerInfo.getName() != null)
                .map(customerInfo -> {
                    SchedulesCustomerDO schedulesCustomerDO = new SchedulesCustomerDO();
                    schedulesCustomerDO.setSchedulesId(schedulesId);
                    schedulesCustomerDO.setCustomerId(customerInfo.getId());
                    schedulesCustomerDO.setCustomerName(customerInfo.getName());
                    return schedulesCustomerDO;
                })
                .collect(Collectors.toList());
    }

}